package com.meihua.workflow.controller;


import com.meihua.workflow.constants.R;
import com.meihua.workflow.model.DeploymentTemplate;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/deployment")
public class DeploymentController {
    @GetMapping
    public R getAllDeployments() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        List<Deployment> deployments = repositoryService.createDeploymentQuery().list();
        ArrayList<DeploymentTemplate> deploymentArrayList = new ArrayList<>();
        DeploymentTemplate deploymentTemplate = new DeploymentTemplate();
        for (Deployment deployment : deployments) {
            deploymentTemplate.setId(deployment.getId());
            deploymentTemplate.setName(deployment.getName());
            deploymentTemplate.setCategory(deployment.getCategory());
            deploymentTemplate.setKey(deployment.getKey());
            deploymentTemplate.setDeploymentTime(deployment.getDeploymentTime());
            deploymentArrayList.add(deploymentTemplate);
        }
        return R.success(deploymentArrayList);
    }
}
